from public.currency import Currency
from public.message import rank_currency_message
from robot.botClient import get_bot_client
from robot.comm.command import FullCommand
from robot.comm.pluginBase import Session


@FullCommand('金币')
async def coin_inquiry(session: Session):
    coin = session.user.query(Currency.coin)
    await session.reply(f'金币数：{coin}', at=True)


@FullCommand('体力')
async def stamina_inquiry(session: Session):
    stamina = session.user.query(Currency.stamina)
    await session.reply(f'体力值：{stamina}', at=True)


async def currency_rank(session: Session, currency: Currency):
    member_list = await session.get_group_member_info_list()
    member_dict = {member['id']: member['memberName'] for member in member_list}
    rank = get_bot_client().send(rank_currency_message.build(currency=currency.name))
    content = '\n'.join(f'{num:5d}\t{member_dict[id]}' for id, num in rank if id in member_dict)
    await session.reply(content)


@FullCommand('金币排行').trim_group('请在群聊中使用此命令')
async def coin_rank(session: Session):
    await currency_rank(session, Currency.coin)


@FullCommand('股票排行').trim_group('请在群聊中使用此命令')
async def stock_rank(session: Session):
    await currency_rank(session, Currency.stock)
